home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
090
/
cmln0485.arc
/
CROSTH1.LTG
< prev
next >
Wrap
Text File
|
1986-02-27
|
3KB
|
99 lines
crosstht. Listing 1.
┴ collectioε oµ procedures¼ writteε iε PPL¼ fo≥ initializing¼ ì
addinτ, anΣ searchinτ fo≥ records
-- Data types will be defined in a Pascal-like style.
-- RECORD : Anytype;
-- KeyData : record Key : Anytype; RecordPointer : integer end;
-- Map : array[1..MAXROW,1..MAXROW] of record First, Last : integer end;
-- Cell : record First, Last : integer end;
-- Bucket : record
-- NumSlot, PreviousBucket, NextBucket : Integer;
-- Slots : array[1..BUCKETSIZE] of (same type as) KeyData
-- end;
PROCEDURE Initialize
OPEN "DATAFILE",1,"RANDOM"
OPEN "KEYFILE",2,"RANDOM"
NData = 0; NBucket = 0;
Zero all elements of Map
END Initialize
PROCEDURE Add
Obtain RECORD
NData += 1; KeyData.RecordPointer = NData;
WRITE 1, RECORD
Row = Hash1(KeyData.Key); Column = Hash2(KeyData.Key)
Cell = Map[Row,Column]
IF Cell.First = 0
THEN -- create a new bucket
[Add_to_New_Bucket]
Map[Row,Column].First = NBucket; Map[Row,Column].Last = NBucket;
ELSE -- Locate last bucket
READ 2, Cell.Last, Bucket
IF Bucket.NumSlot = BUCKETSIZE -- Is the bucket full?
THEN -- Create a new bucket and maintain linked list
Bucket.NextBucket = NBucket + 1; WRITE 2, Cell.Last,Bucket
[Add_to_New_Bucket]
Map[Row,Column].Last = NBucket -- keep track of last bucket in list
ELSE -- Add in the same bucket
Bucket.NumSlot += 1
Bucket.Slots[Bucket.NumSlot] = KeyData
WRITE 2, Cell.Last,Bucket
END IF;
END IF;
èPROCEDURE Add_to_New_Bucket
NBucket += 1; Bucket.NumSlot = 1;
Bucket.PreviousBucket = Cell.Last -- pointer to previous bucket or zero if
-- this is the first bucket in the list
Bucket.NextBucket = 0 -- zero indicates end of linked list.
Bucket.Slots[1] = KeyData
WRITE 2, NBucket, Bucket
END Add_to_New_Bucket
PROCEDURE Search;
-- Seacrh through buckets.
Obtain SearchKey
Row = Hash1(SearchKey); Column = Hash2(SearchKey)
Cell = Map[Row,Column]
IF Cell.First = 0 -- sought data is definitely not on file
THEN
DISPLAY "Data nonexistent"
ELSE
INITIALIZE: FoundFlag = False; NextOne = Cell.First
LOOP <BIG>
BEGIN
READ 2, NextOne, Bucket
INITIALIZE: None
LOOP <Look_in_a_Bucket>
BEGIN for i = 1 to NumSlot
IF Slots[i].Key = SearchKey THEN FoundFlag = True; EXIT <BIG> END IF;
END LOOP <Look_in_a_Bucket>;
TERMINATE: NextOne = Bucket.NextBucket
IF NextOne = 0 THEN EXIT <BIG> END IF; -- when end of link is found
-- exit loop
END LOOP <BIG>;
TERMINATE: IF FoundFlag
THEN
DISPLAY "Data in record # ";Slot[i].RecordPointer
READ 1, Slot[i].RecordPointer, RECORD
-- Perform more data processing of your choice
ELSE
DISPLAY "Data nonexistent"
END IF;
END Search